package com.labtalk.admin.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.labtalk.admin.mapper.BarcodeMapper;
import com.labtalk.admin.pojo.common.PageVo;
import com.labtalk.admin.pojo.common.ResponseVo;
import com.labtalk.admin.pojo.entity.BarcodeEntity;
import com.labtalk.admin.pojo.model.BarcodeModel;
import com.labtalk.admin.service.BarcodeService;
import com.labtalk.admin.utils.BarcodeUtil;
import com.labtalk.admin.utils.FileUtil;
import com.labtalk.admin.utils.UuidUtil;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Service
public class BarcodeImpl implements BarcodeService {

    @Autowired
    private BarcodeMapper barcodeMapper;

    @Override
    public ResponseVo getList(Page<BarcodeEntity> page) {
        Page<BarcodeEntity> data = barcodeMapper.selectList(page);
        PageVo<BarcodeEntity> content = new PageVo<>(data);
        return ResponseVo.success(200, "success", content);
    }

    @Override
    // 创建条形码
    public ResponseVo create(BarcodeModel model) {
        model.setId(UuidUtil.simpleUUID());
        String barcode = BarcodeUtil.getBarcode(model);
        model.setCode(barcode); // 条形码
        barcodeMapper.insert(model);
        return ResponseVo.success(200, "success", "");
    }

    @Override
    // 更新条形码
    public ResponseVo update(BarcodeModel model) {
        String barcode = BarcodeUtil.getBarcode(model);
        model.setCode(barcode); // 条形码
        barcodeMapper.update(model);
        return ResponseVo.success(200, "success", "");
    }

    @Override
    // 删除条形码
    public ResponseVo delete(String id) {
        barcodeMapper.delete(id);
        return ResponseVo.success(200, "success", "");
    }

    @Override
    public ResponseVo upload(MultipartFile file) throws IOException {
        ImportParams importParams = new ImportParams();
        List<BarcodeModel> list = ExcelImportUtil.importExcel(FileUtil.convertMultipartFileToFile(file), BarcodeModel.class, importParams);
        list.forEach(item -> {
            item.setId(UuidUtil.simpleUUID());
            // 先处理有效期
            Calendar calendar = new GregorianCalendar(1900,0,-1);
            int intDay = Integer.parseInt(String.valueOf(item.getValidity()));
            Date validity = DateUtils.addDays(calendar.getTime(),intDay);
            item.setValidity(validity.getTime());
            // 再生成条形码
            String barcode = BarcodeUtil.getBarcode(item);
            item.setCode(barcode); // 完整条形码
        });
        barcodeMapper.insertMultiple(list);
        return ResponseVo.success(200, "success", "");
    }
}
